package com.yunhe.transfer.controller.data;

import com.yunhe.common.model.ResultObject;
import com.yunhe.transfer.service.data.DpuFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * DPU采集和控制配置文件下发、查看Controller
 *
 * @author duanxiaoyong
 */
@Api(value = "DpuFile", description = "DPU控制配置文件下发和查看", tags = {"Data_DpuFile"})
@RestController
@RequestMapping("/dpuFiles")
public class DpuFileController {

    @Autowired
    private DpuFileService dpuFileService;

    /**
     * DPU文件下发
     */
    @ApiOperation("文件下发")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文件类型（Channel 通道,Point 测点,AOE AOE）", dataType = "string", paramType = "query", required = true, allowableValues = "Channel,Point,AOE"),
            @ApiImplicitParam(name = "stationId", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "beeId", value = "设备ID", dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> send(@RequestParam("type") String type,
                                             @RequestParam("stationId") Optional<Long> stationId,
                                             @RequestParam("beeId") Optional<String> beeId,
                                             @RequestParam("file") MultipartFile file)
            throws Exception {
        Boolean result = dpuFileService.send(type, stationId, beeId, file);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * DPU文件查看
     */
    @ApiOperation("查看文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文件类型（Channel 通道,Point 测点,AOE AOE）", dataType = "string", paramType = "query", required = true, allowableValues = "Channel,Point,AOE"),
            @ApiImplicitParam(name = "beeId", value = "设备ID", dataType = "long", paramType = "query", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void search(@RequestParam("type") String type,
                       @RequestParam("beeId") String beeId,
                       @RequestParam("fileName") Optional<String> fileName,
                       HttpServletResponse response) throws Exception {
        dpuFileService.export(type, beeId, fileName.orElse(null), response);
    }
}
